package com.oceanread.cn.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.oceanread.cn.entity.Projections;
import com.oceanread.cn.entity.ProjectionsTools;
import com.oceanread.cn.service.IProjectionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/Projections")
@CrossOrigin()
@Api(value = "海底管线勘测项目表", tags = {"2-海底管线勘测项目表"})
public class ProjrctionsController {

    @Autowired
    private IProjectionsService iProjectionsService;

    @ApiIgnore(value = "2.1 获取总表____前端可能有用")
    @GetMapping("/getall")
    public List<Projections> getAll() {
        return iProjectionsService.getall();
    }

    /*卫杰前端接口   没有对应的表格*/
    @ApiOperation(value = "2.2 前端可视化接口__获取数据")
    @GetMapping("/getPrjList")
    public JSONArray getPrjList() {
        JSONArray array = new JSONArray();
        ProjectionsTools projectionsTools = new ProjectionsTools();
        for (Projections projections : iProjectionsService.getall()) {
            JSONObject object = new JSONObject();
            object = projectionsTools.getinfo(projections);
            array.add(object);
        }
        return array;
    }

    @ApiOperation(value = "1.1 项目列表____分页")
    @GetMapping("/findall")
    public JSONObject findall(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                              @RequestParam(name = "size", required = true, defaultValue = "10") int size) {
        JSONObject object = new JSONObject();
        object.put("data", iProjectionsService.findall(page, size));
        object.put("total", iProjectionsService.getnum());
        return object;
    }

    //    @PostMapping("/query")
////    public List<Projections> query(@RequestBody Query query){
////        return iProjectionsService.query(query.getKey(),query.getValue());
////    }
    @ApiOperation(value = "模糊查询____1参数+分页")
    @GetMapping("/query")
    public JSONObject query(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                            @RequestParam(name = "size", required = true, defaultValue = "10") int size,
                            @RequestParam(name = "key") String key,
                            @RequestParam(name = "value") String value) {
        JSONObject object = new JSONObject();
        object.put("data", iProjectionsService.query(page, size, key, value));
        object.put("total", iProjectionsService.getnumquery(key, value));
        return object;
    }

    @ApiOperation(value = "1.3 项目信息更新")
    @PostMapping("/update")
    public Integer update(@RequestBody Projections projections) {
        return iProjectionsService.update(projections);
    }

    @ApiOperation(value = "1.2 项目信息插入")
    @PostMapping("/insert")
    public Integer insert(@RequestBody Projections projections) {
        return iProjectionsService.insert(
                projections.getPrjCode(), projections.getName(),
                projections.getShortName(), projections.getDate(),
                projections.getLon(), projections.getLat(), projections.getType(),
                projections.getCameraHight(), projections.getNumPipeline(),
                projections.getLengthPipeline(), projections.getDepthPipeline(),
                projections.getNumDrillhole(), projections.getNumProfile(),
                projections.getUndertakingUnit()
        );
    }

    @ApiOperation(value = "1.4 项目信息删除")
    @GetMapping("/delete")
    public Integer delete(@RequestParam(name = "SYSID") int SYSID) {
        return iProjectionsService.delete(SYSID);
    }

}
